angular.module('mamApp').controller('messageCreateController', ['$scope', 'NoticeManager', 'MessageManager', '$state', '$uibModal', 'ImsDialog', '$filter', '$sce',
    function ($scope, NoticeManager, MessageManager, $state, $uibModal, ImsDialog, $filter, $sce) {
        $scope.$sce = $sce;

        $scope.titleStyles = [{key: 301, value: $scope.table[301]}, {key: 302, value: $scope.table[302]}, {
            key: 303,
            value: $scope.table[303]
        }];
        $scope.Lvs = [{key: 20, value: $scope.table[20]}, {key: 21, value: $scope.table[21]}, {
            key: 22,
            value: $scope.table[22]
        }];


        $scope.signs = [{key: 30, value: $scope.table[30]}, {key: 31, value: $scope.table[31]}];
        $scope.checkStatus = [{key: 7, value: $scope.table[7]}, {key: 8, value: $scope.table[8]}, {
            key: 9,
            value: $scope.table[9]
        }];
        $scope.rangeDate = {
            startDate: $scope.search.startDay ? moment($scope.search.startDay).format('X') : null,
            endDate: $scope.search.endDay ? moment($scope.search.endDay).format('X') : null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.formData.startDay = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.formData.endDay = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;

            }
        };
        $scope.selectCPs = [];

        //model 点击打开。param：title文字、body数据、选择触发回调方法，size：model大小（big、small，默认中等大小）
        $scope.cpSelect = function (param, elm, size) {
            var modal = $uibModal.open({
                templateUrl: 'cpSelectModal.html',
                controller: 'CPSelectModalController',
                resolve: {
                    param: function () {
                        return {Lvs: $scope.Lvs, signs: $scope.signs}
                    }
                }
            });
            modal.result.then(function (result) {
                result.forEach(function (item) {
                    $filter('filter')($scope.selectCPs, {cpId: item.cpId}).length ? null : $scope.selectCPs.push(item);
                })
            });
        };

        $scope.deleteSelectCP = function (index) {
            $scope.selectCPs.splice(index, 1);
        }

        var messageId = $state.params.messageId;
        if (messageId) {
            MessageManager.messageDetail(messageId).then(function (result) {
                $scope.messageDetail = result.data.messageVO;
                console.log($scope.messageDetail)
            })
        }


        //创建公告
        $scope.createMessage = function () {
            var titleStyleSelect = [];
            var CPs = [];
            $scope.selectCPs.forEach(function (cp) {
                CPs.push(cp.cpId);
            })
            $scope.titleStyles.forEach(function (titleStyle) {
                if (titleStyle.checked) {
                    titleStyleSelect.push(titleStyle.key);
                }
            })

            $scope.formData.receiveUserId = CPs.join(",");
            $scope.formData.charMark = titleStyleSelect.join(",");
            MessageManager.messageAdd($scope.formData).then(function (result) {
                if (result.data.c == "s") {
                    $state.go("main.message", {}, {reload: true})
                }
            })


        }


    }])

